/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.Map;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;

public class EdgeNGramTokenFilter extends ElasticFilter {

	

	
	public static final String MIN_GRAM_PARAM_KEY = "min_gram";
	public static final String MAX_GRAM_PARAM_KEY = "max_gram";
	public static final String SIDE_PARAM_KEY = "side";
	
	public static enum Side{
		front, back
	}
	
	private Integer minGram;
	private Integer maxGram;
	private Side side;
	
	
	public EdgeNGramTokenFilter(String defName) {
		this(null, defName);
	}

	public EdgeNGramTokenFilter(Map<String,String> params, String defName) {
		super(ElasticFilterType.edgeNGram, defName);
		
		if(params!=null){
			String mingStr = params.get(MIN_GRAM_PARAM_KEY);
			if(mingStr!=null && !mingStr.isEmpty()){
				try {
					int ming = Integer.parseInt(mingStr);
					this.minGram = ming;
				} catch (NumberFormatException e) {
				}
			}
			
			String maxgStr = params.get(MAX_GRAM_PARAM_KEY);
			if(maxgStr!=null && !maxgStr.isEmpty()){
				try {
					int maxg = Integer.parseInt(maxgStr);
					this.maxGram = maxg;
				} catch (NumberFormatException e) {
				}
			}
			
			String sideStr = params.get(SIDE_PARAM_KEY);
			if(sideStr!=null && !sideStr.isEmpty()){
				try {
					Side s = Side.valueOf(sideStr);
					this.side = s;
				} catch (Exception e) {
				}				
			}
			
		}
	}
	
	

	@Override
	public boolean hasCustomParams() {
		 if(this.minGram!= null || this.maxGram!=null || this.side!=null )return true;
		 return false;
	}
	
	@Override
	public XContentBuilder addToBuilder(
			XContentBuilder mappingBuilder) throws IOException {

		mappingBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		
		if(this.minGram != null){
			mappingBuilder.field(MIN_GRAM_PARAM_KEY, this.minGram);
		}
		
		if(this.maxGram != null){
			mappingBuilder.field(MAX_GRAM_PARAM_KEY, this.maxGram);
		}
		
		if (this.side != null){
			mappingBuilder.field(SIDE_PARAM_KEY, this.side.toString());
		}
		
		return mappingBuilder;
	}
	
	
	@Override
	public void setParamsFromSettings(Settings tokenizerSettings) {
		
		String minGSettings = tokenizerSettings.get(MIN_GRAM_PARAM_KEY);
		if(minGSettings!=null && !minGSettings.isEmpty()){
			try {
				int mtl = Integer.parseInt(minGSettings);
				this.minGram = mtl;
			} catch (NumberFormatException e) {
			}
		}
		
		String maxGSettings = tokenizerSettings.get(MAX_GRAM_PARAM_KEY);
		if(maxGSettings!=null && !maxGSettings.isEmpty()){
			try {
				int mtl = Integer.parseInt(maxGSettings);
				this.maxGram = mtl;
			} catch (NumberFormatException e) {
			}
		}
		
		String sideSettings = tokenizerSettings.get(SIDE_PARAM_KEY);
		if(sideSettings!=null && !sideSettings.isEmpty()){
			try {
				Side s = Side.valueOf(sideSettings);
				this.side = s;
			} catch (NumberFormatException e) {
			}
		}
	}
	
	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition otherTok){
		
		if(otherTok==null)return false;
		EdgeNGramTokenFilter ot ;
		try {
			ot = (EdgeNGramTokenFilter)otherTok;
		} catch (ClassCastException e) {
			return false;
		}
		
		
		return 
		EqualsUtil.areEqual(this.minGram, ot.minGram)
		&&
		EqualsUtil.areEqual(this.maxGram, ot.maxGram)
		&&
		EqualsUtil.areEqual(this.side, ot.side);
	}
		
	
	
	
	
}
